package com.example.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.toolkit.Db;
import com.example.domian.enums.UserStatus;
import com.example.domian.po.Address;
import com.example.domian.po.Result;
import com.example.domian.vo.AddressVo;
import com.example.domian.vo.UserVO;
import com.example.service.UserService;
import com.example.mapper.UserMapper;
import com.example.domian.po.User;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * ClassName: UserServiceImpl
 * Package: com.example.Service.impl
 * Description:
 *
 * @Author: yyx
 * @Create: 2025/7/14 - 15:49
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Override
    public void deductBalance(Long id, Integer money) {
        User user = getById(id);
        if(user==null||user.getStatus()==UserStatus.FREEZE){
            throw new RuntimeException("用户状态异常");
        }
        if(user.getBalance()<money){
            throw  new RuntimeException("用户余额不足");
        }
        baseMapper.deductMoneyById(id,money);
    }

    @Override
    public UserVO queryUserAndAddressById(Long userId) {
        User user = getById(userId);
        if(user==null||user.getStatus()== UserStatus.FREEZE){
            throw  new RuntimeException("用户状态异常");
        }
        List<Address> list = Db.lambdaQuery(Address.class).eq(Address::getUserId,userId).list();
        System.out.println("这是list："+list);
        UserVO userVO = BeanUtil.copyProperties(user, UserVO.class);
        if(CollUtil.isNotEmpty(list)){
            List<AddressVo> addressVos = BeanUtil.copyToList(list, AddressVo.class);
            userVO.setAddressVos(addressVos);
        }

        return userVO;
    }

    @Override
    public Result queryUserByIdList(List<Long> ids) {
        List<User> users = listByIds(ids);
        if(CollUtil.isEmpty(users)){
            List<Object> emptyList = Collections.emptyList();
            return Result.success(emptyList);
        }
        List<Long> userIds = users.stream().map(User::getId).collect(Collectors.toList());
        List<Address> address = Db.lambdaQuery(Address.class).in(Address::getUserId, userIds).list();
        List<AddressVo> addressVoList = BeanUtil.copyToList(address, AddressVo.class);
        Map<Long, List<AddressVo>> addressMap= new HashMap<>(0);
        if(CollUtil.isNotEmpty(addressVoList)){
            addressMap = addressVoList.stream()
                    .collect(Collectors.groupingBy(AddressVo::getUserId));
        }
        List<UserVO> list =new ArrayList<>(users.size());
        for (User user : users) {
            UserVO userVO = BeanUtil.copyProperties(user, UserVO.class);
            list.add(userVO);
            userVO.setAddressVos(addressMap.get(user.getId()));
        }
        return Result.success(list);
    }
}
